home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir37 / ms_sh23s.zip / SRC / SHOWKEY.C < prev    next >
C/C++ Source or Header  |  1994-08-26  |  7KB  |  311 lines

  1. /* MS-DOS SHELL - Show Scan codes
  2.  *
  3.  * MS-DOS SHELL - Copyright (c) 1990,4 Data Logic Limited.
  4.  *
  5.  * This code is subject to the following copyright restrictions:
  6.  *
  7.  * 1.  Redistribution and use in source and binary forms are permitted
  8.  *     provided that the above copyright notice is duplicated in the
  9.  *     source form and the copyright notice in file sh6.c is displayed
  10.  *     on entry to the program.
  11.  *
  12.  * 2.  The sources (or parts thereof) or objects generated from the sources
  13.  *     (or parts of sources) cannot be sold under any circumstances.
  14.  *
  15.  *    $Header: /usr/users/istewart/shell/sh2.3/Release/RCS/showkey.c,v 2.5 1994/08/25 20:49:11 istewart Exp $
  16.  *
  17.  *    $Log: showkey.c,v $
  18.  * Revision 2.5  1994/08/25  20:49:11  istewart
  19.  * MS Shell 2.3 Release
  20.  *
  21.  * Revision 2.4  1994/02/01  10:25:20  istewart
  22.  * Release 2.3 Beta 2, including first NT port
  23.  *
  24.  * Revision 2.3  1994/01/11  17:55:25  istewart
  25.  * Release 2.3 Beta 0 patches
  26.  *
  27.  * Revision 2.2  1993/06/14  10:59:58  istewart
  28.  * More changes for 223 beta
  29.  *
  30.  * Revision 2.1  1993/06/02  09:54:34  istewart
  31.  * Beta 223 Updates - see Notes file
  32.  *
  33.  * Revision 2.0  1992/07/16  14:35:08  istewart
  34.  * Release 2.0
  35.  *
  36.  *
  37.  */
  38.  
  39. #include <stdio.h>
  40. #include <unistd.h>
  41. #include <ctype.h>
  42.  
  43. #if defined (OS2) || defined (__OS2__)
  44. #  define INCL_KBD
  45. #  include <os2.h>
  46.  
  47. #  if defined (__OS2__)
  48. #    include <bsedev.h>
  49. #  endif
  50.  
  51. #elif (WIN32)
  52. #  include <windows.h>
  53. WORD IgnoreScanCode[] = { 0x1d, 0x38, 0x2a, 0x36, 0x3a, 0x45 };
  54. #define IGNORE_CNT    (sizeof (IgnoreScanCode) / sizeof (IgnoreScanCode[0]))
  55. #else
  56. #  include <dos.h>
  57.  
  58. extern void    CheckExtendedKeyboard (void);
  59. unsigned char    KeyInt = 0;
  60. #endif
  61.  
  62. void main (void)
  63. {
  64. #if defined (OS2) || defined (__OS2__)
  65.     KBDKEYINFO        kbci;
  66.     KBDINFO        kbstInfo;
  67.     unsigned        rc;
  68. #elif defined (WIN32)
  69.     INPUT_RECORD    Buffer;
  70.     DWORD        NumberOfEventsRead;
  71.     int            i;
  72. #else
  73.     union REGS    Key;
  74.     union REGS    Shift;
  75. #endif
  76.  
  77. #if defined (OS2) || defined (__OS2__)
  78.     kbstInfo.cb = sizeof (kbstInfo);
  79.     if (rc = KbdGetStatus (&kbstInfo, 0))    /* get current status    */
  80.     {
  81.          fprintf (stderr, "KbdGetStatus failed - %d\n", rc);
  82.      exit (1);
  83.     }
  84.  
  85.     kbstInfo.fsMask = (kbstInfo.fsMask &
  86.                 ~(KEYBOARD_ASCII_MODE | KEYBOARD_ECHO_ON)) |
  87.                (KEYBOARD_ECHO_OFF | KEYBOARD_BINARY_MODE);
  88.  
  89.     if (rc = KbdSetStatus (&kbstInfo, 0))
  90.          fprintf (stderr, "KbdSetStatus failed - %d\n", rc);
  91. #elif defined (WIN32)
  92.     SetConsoleMode (GetStdHandle (STD_INPUT_HANDLE), 0);
  93. #else
  94.     CheckExtendedKeyboard ();
  95. #endif
  96.  
  97.     puts ("Control C to terminate");
  98.  
  99.     while (TRUE)
  100.     {
  101. #if defined (OS2) || defined (__OS2__)
  102.  
  103.     KbdCharIn (&kbci, IO_WAIT, 0);
  104.  
  105.     printf ("Scan = 0x%.4x ", kbci.chScan);
  106.     printf ("ASCII = 0x%.4x (%c) ", kbci.chChar, isprint (kbci.chChar) ?
  107.         kbci.chChar : '.');
  108.  
  109.     printf ("Shift = 0x%.4x ( ", kbci.fsState);
  110.  
  111.     if (kbci.fsState & RIGHTSHIFT)
  112.         printf ("RIGHTSHIFT ");
  113.  
  114.     if (kbci.fsState & LEFTSHIFT)
  115.         printf ("LEFTSHIFT ");
  116.  
  117.     if (kbci.fsState & ALT)
  118.         printf ("ALT ");
  119.  
  120.     if (kbci.fsState & LEFTALT)
  121.         printf ("LEFTALT ");
  122.  
  123.     if (kbci.fsState & RIGHTALT)
  124.         printf ("RIGHTALT ");
  125.  
  126.     if (kbci.fsState & CONTROL)
  127.         printf ("CONTROL ");
  128.  
  129.     if (kbci.fsState & LEFTCONTROL)
  130.         printf ("LEFTCONTROL ");
  131.  
  132.     if (kbci.fsState & RIGHTCONTROL)
  133.         printf ("RIGHTCONTROL ");
  134.  
  135.     if (kbci.fsState & SCROLLLOCK_ON)
  136.         printf ("SCROLLLOCK_ON ");
  137.  
  138.     if (kbci.fsState & SCROLLLOCK)
  139.         printf ("SCROLLLOCK ");
  140.  
  141.     if (kbci.fsState & NUMLOCK_ON)
  142.         printf ("NUMLOCK_ON ");
  143.  
  144.     if (kbci.fsState & NUMLOCK)
  145.         printf ("NUMLOCK ");
  146.  
  147.     if (kbci.fsState & CAPSLOCK_ON)
  148.         printf ("CAPSLOCK_ON ");
  149.  
  150.     if (kbci.fsState & CAPSLOCK)
  151.         printf ("CAPSLOCK ");
  152.  
  153.     if (kbci.fsState & INSERT_ON)
  154.         printf ("INSERT_ON ");
  155.  
  156.     if (kbci.fsState & SYSREQ)
  157.         printf ("SYSREQ ");
  158.  
  159.     puts (")");
  160.  
  161.     if (kbci.chChar == 0x03)
  162.         exit (0);
  163. #elif defined (WIN32)
  164.  
  165.     ReadConsoleInput (GetStdHandle (STD_INPUT_HANDLE), &Buffer, 1,
  166.               &NumberOfEventsRead);
  167.  
  168.     for (i = 0; i < IGNORE_CNT; i++)
  169.     {
  170.         if (Buffer.Event.KeyEvent.wVirtualScanCode == IgnoreScanCode[i])
  171.             break;
  172.     }
  173.  
  174.     if (i != IGNORE_CNT)
  175.         continue;
  176.  
  177.     printf ("KeyCode = 0x%.4x Scan = 0x%.4x Down %d Repeat %d\n",
  178.         Buffer.Event.KeyEvent.wVirtualKeyCode,
  179.         Buffer.Event.KeyEvent.wVirtualScanCode,
  180.         Buffer.Event.KeyEvent.bKeyDown,
  181.         Buffer.Event.KeyEvent.wRepeatCount);
  182.     
  183.     printf ("ASCII = 0x%.4x (%c) ", Buffer.Event.KeyEvent.uChar.AsciiChar,
  184.         isprint (Buffer.Event.KeyEvent.uChar.AsciiChar)
  185.             ?  Buffer.Event.KeyEvent.uChar.AsciiChar
  186.             : '.');
  187.  
  188.     printf ("Shift = 0x%.4x ( ", Buffer.Event.KeyEvent.dwControlKeyState);
  189.  
  190.     if (Buffer.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
  191.         printf ("SHIFT_PRESSED ");
  192.  
  193.     if (Buffer.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED)
  194.         printf ("LEFTALT ");
  195.  
  196.     if (Buffer.Event.KeyEvent.dwControlKeyState & RIGHT_ALT_PRESSED)
  197.         printf ("RIGHTALT ");
  198.  
  199.     if (Buffer.Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED)
  200.         printf ("LEFTCONTROL ");
  201.  
  202.     if (Buffer.Event.KeyEvent.dwControlKeyState & RIGHT_CTRL_PRESSED)
  203.         printf ("RIGHTCONTROL ");
  204.  
  205.     if (Buffer.Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON)
  206.         printf ("SCROLLLOCK_ON ");
  207.  
  208.     if (Buffer.Event.KeyEvent.dwControlKeyState & NUMLOCK_ON)
  209.         printf ("NUMLOCK_ON ");
  210.  
  211.     if (Buffer.Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON)
  212.         printf ("CAPSLOCK_ON ");
  213.  
  214.     if (Buffer.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY)
  215.         printf ("ENHANCED_KEY ");
  216.  
  217.     puts (")");
  218.  
  219.     if (Buffer.Event.KeyEvent.uChar.AsciiChar == 0x03)
  220.         exit (0);
  221.  
  222. #else
  223.     Key.h.ah = KeyInt;
  224. #if defined (__WATCOMC__) && defined(__386__) && !defined(__WINDOWS_386__)
  225.     int386 (0x16, &Key, &Key);
  226. #else
  227.     int86 (0x16, &Key, &Key);
  228. #endif
  229.  
  230.     Shift.h.ah = 0x02;
  231. #if defined (__WATCOMC__) && defined(__386__) && !defined(__WINDOWS_386__)
  232.     int386 (0x16, &Shift, &Shift);
  233. #else
  234.     int86 (0x16, &Shift, &Shift);
  235. #endif
  236.  
  237.     printf ("Scan = 0x%.4x ", Key.h.ah);
  238.     printf ("ASCII = 0x%.4x (%c) ", Key.h.al, isprint (Key.h.al) ?
  239.         Key.h.al : '.');
  240.     printf ("Shift = 0x%.4x ( ", Shift.h.al);
  241.  
  242.     if (Shift.h.al & 0x01)
  243.         printf ("Right Shift ");
  244.  
  245.     if (Shift.h.al & 0x02)
  246.         printf ("Left Shift ");
  247.  
  248.     if (Shift.h.al & 0x04)
  249.         printf ("Control ");
  250.  
  251.     if (Shift.h.al & 0x08)
  252.         printf ("Alt ");
  253.  
  254.     puts (")");
  255.  
  256.     if (Key.h.al == 0x03)
  257.         exit (0);
  258. #endif
  259.     }
  260. }
  261.  
  262. /*
  263.  * Check for extended keyboard
  264.  */
  265.  
  266. #if !defined (OS2) && !defined (__OS2__) && !defined (WIN32)
  267. void    CheckExtendedKeyboard ()
  268. {
  269.     union REGS    r;
  270.     int        i;
  271.  
  272.     r.h.ah = 0x05;
  273.  
  274. #if defined (__WATCOMC__) && defined(__386__) && !defined(__WINDOWS_386__)
  275.     r.x.ecx = 0xffff;
  276.     int386 (0x16, &r, &r);
  277. #else
  278.     r.x.cx = 0xffff;
  279.     int86 (0x16, &r, &r);
  280. #endif
  281.  
  282.     if (r.h.al)
  283.     {
  284.     fprintf (stderr, "showkey: keyboard full!!\n");
  285.     return;
  286.     }
  287.  
  288.     for (i = 0; i < 16; i++)
  289.     {
  290.     r.h.ah = 0x10;
  291. #if defined (__WATCOMC__) && defined(__386__) && !defined(__WINDOWS_386__)
  292.     int386 (0x16, &r, &r);
  293.  
  294.     if (r.x.eax & 0x0ffff == 0x0ffff)
  295. #else
  296.     int86 (0x16, &r, &r);
  297.  
  298.     if (r.x.ax == 0xffff)
  299. #endif
  300.     {
  301.         KeyInt = 0x10;
  302.         puts ("showkey: Extended keyboard detected");
  303.         return;
  304.     }
  305.     }
  306.  
  307.     puts ("showkey: Normal keyboard detected");
  308.     return;
  309. }
  310. #endif
  311.